//
//  ViewController.m
//  UITableView多列展示
//
//  Created by 高得华 on 16/9/2.
//  Copyright © 2016年 GaoFei. All rights reserved.
//

#import "ViewController.h"
#import "GFMCTableViewCell.h"
#import "GFMultipleColumnsTVCell.h"
#import "MyNetworking.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) NSMutableDictionary * dataDictionary;
@property (nonatomic,strong) NSArray * allkeys;
@property(nonatomic,strong)NSArray *titleArr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    //返回多少个大组
    self.titleArr = @[@"旅游城市",@"娱乐",@"微商素材"];
    self.dataDictionary = [NSMutableDictionary dictionary];
    for (NSString * keyStr in self.titleArr) {
        NSMutableArray * array = [NSMutableArray array];
        for (int i = 1; i <= 9; i++) {
            [array addObject:[NSString stringWithFormat:@"%2d",i]];
        }
        [self.dataDictionary setObject:array forKey:keyStr];
    }
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    [self.view addSubview:tableView];
    tableView.delegate = self;
    tableView.dataSource = self;
    //获取热门城市列表
    [self getHotCityVidoList];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GFMCTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"GFMCTableViewCell"];
    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle] loadNibNamed:@"GFMCTableViewCell" owner:nil options:nil] objectAtIndex:0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    cell.indexPath = indexPath;
    NSString * keyStr = self.titleArr[indexPath.section];
    NSArray * array = [self.dataDictionary objectForKey:keyStr];
    cell.dataArray = array;
    
    // cell.tvCellView.ReturnClickItemIndex = ^(NSIndexPath * itemtIP ,NSInteger itemIndex){
    //   NSLog(@"----###----###---(%ld,%ld)----##---%ld----###-----",itemtIP.section,itemtIP.row,itemIndex);
    //};
    
    
    
    return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return self.titleArr[section];
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CGFloat cellHt = 0.0;
    NSString * keyStr = self.titleArr[indexPath.section];
    NSArray * array = [self.dataDictionary objectForKey:keyStr];
    if (array.count != 0) {
        GFMultipleColumnsTVCell * cellView = [[GFMultipleColumnsTVCell alloc] init];
        cellView.dataArrayCount = array.count;
        cellHt += cellView.cellHeight;
    }
    
    return cellHt;
}
//获取热门城市视频列表
-(void)getHotCityVidoList{
    [[MyNetworking sharedManager]obtainCityClassWithSuccessBlock:^(NSDictionary *dic) {
        int code = [[dic valueForKey:@"code"] integerValue];
        if (code==1) {
            NSArray *hotCityArr = [dic valueForKey:@"data"];
            //获取热门视频导航
            [[NSUserDefaults standardUserDefaults]setObject:hotCityArr forKey:@"hotCityArr"];
            [[NSUserDefaults standardUserDefaults]synchronize];
        }
        
    } WithFailurBlock:^(NSError *error) {
        
    }];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
//邮编的字母排序
//- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
//  return self.allkeys;
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
